/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TPurchase;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TPurchaseDao;
import com.ruipengkj.service.TPurchaseService;
/**
 * 说明：
 * 采购Service层接口实现
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TPurchaseServiceImpl implements TPurchaseService{
	@Resource
	private TPurchaseDao TPurchaseDao;//DAO层
	//
	@Override
	public List<TPurchase> findService(TPurchase entity) throws Exception {
		String hql = "select o from TPurchase o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getPurchaseID()!=null&&!entity.getPurchaseID().equals("")){
				whereHQL.append("purchaseID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getPurchaseID()+"%");
			}
			if(entity.getBuyer()!=null&&!entity.getBuyer().equals("")){
				whereHQL.append("buyer").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBuyer()+"%");
			}
			//按时间查询
			if(null!=entity.getHappenTime()&&!("").equals(entity.getHappenTime())){
				whereHQL.append("HappenTime>=?").append(BaseQuery.AND);
				param.add(entity.getHappenTime());
			}
			if(null!=entity.getEndTime()&&!("").equals(entity.getEndTime())){
				whereHQL.append("HappenTime<=?").append(BaseQuery.AND);
				param.add(entity.getEndTime());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TPurchaseDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	//根据条件翻页查询
	@Override
	public PageModel<TPurchase> getScrollService(int firstResult,
			int maxResult, TPurchase entity) throws Exception {
		String hql = "select o from TPurchase o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getPurchaseID()!=null&&!entity.getPurchaseID().equals("")){
				whereHQL.append("purchaseID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getPurchaseID()+"%");
			}
			if(entity.getBuyer()!=null&&!entity.getBuyer().equals("")){
				whereHQL.append("buyer").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBuyer()+"%");
			}
			//按时间查询
			if(null!=entity.getHappenTime()&&!("").equals(entity.getHappenTime())){
				whereHQL.append("HappenTime>=?").append(BaseQuery.AND);
				param.add(entity.getHappenTime());
			}
			if(null!=entity.getEndTime()&&!("").equals(entity.getEndTime())){
				whereHQL.append("HappenTime<=?").append(BaseQuery.AND);
				param.add(entity.getEndTime());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TPurchaseDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	
	@Override
	public PageModel<TPurchase> getScrollListService(int firstResult,
			int maxResult, TPurchase entity, List<TEmployee> tEmployeeList) throws Exception {
		String hql = "select new TPurchase(o.purchaseID, o.buyer, o.operator,"+
			"o.happenTime, o.TSupplier.name, o.TSupplier.mobile, o.TSupplier.phone, o.TSupplier.contact) from TPurchase o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getPurchaseID()!=null&&!entity.getPurchaseID().equals("")){
				whereHQL.append("o.purchaseID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getPurchaseID()+"%");
			}
			if (tEmployeeList != null && tEmployeeList.size() != 0) {
				whereHQL.append("(");
				for (int i = tEmployeeList.size() - 1; i >= 0; --i) {
					whereHQL.append("o.buyer").append(BaseQuery.LIKE).append("?").append(BaseQuery.OR);
					param.add("%"+tEmployeeList.get(i).getEmployeeID()+"%");
				}
				whereHQL.delete(whereHQL.length()-3, whereHQL.length());
				whereHQL.append(") "+BaseQuery.AND);
			}
			//按时间查询
			if(null!=entity.getHappenTime()&&!("").equals(entity.getHappenTime())){
				whereHQL.append("o.happenTime>=?").append(BaseQuery.AND);
				param.add(entity.getHappenTime());
			}
			if(null!=entity.getEndTime()&&!("").equals(entity.getEndTime())){
				whereHQL.append("o.happenTime<=?").append(BaseQuery.AND);
				param.add(entity.getEndTime());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TPurchaseDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	
	//修改
	@Override
	public void modifyService(TPurchase entity) throws Exception {
		TPurchaseDao.modifyDao(entity);
		
	}
	//添加
	@Override
	public void saveService(TPurchase entity) throws Exception {
		TPurchaseDao.saveDao(entity);
	}
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TPurchaseDao.deleteDao(id);
		
	}
	//根据ID查询
	@Override
	public TPurchase getByIdService(String id) throws Exception {
		return TPurchaseDao.getByIdDao(id);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TPurchase entity) throws Exception {
		TPurchaseDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TPurchase entity) throws Exception {
		return TPurchaseDao.saveRetDao(entity);
	}
	public TPurchaseDao getTPurchaseDao() {
		return TPurchaseDao;
	}

	public void setTPurchaseDao(TPurchaseDao TPurchaseDao) {
		this.TPurchaseDao = TPurchaseDao;
	}
	
	@Override
	public Object executeHQL() throws Exception {
//		String hql = "drop table TPurchase";
		String hql = "delete from TPurchase";
		return TPurchaseDao.executeHQLDao(hql, "", "", null);
	}
	
	@Override
	public int executeSQL(String sql) {
		return TPurchaseDao.executeSQL(sql);
	}

	@Override
	public List<Object[]> querySQL(String sql) {
		return TPurchaseDao.querySQL(sql);
	}
}